//
//  SCHomeClassifyV.m
//  shangcheng
//
//  Created by Jie Jie on 2019/6/14.
//  Copyright © 2019 张艳江. All rights reserved.
//

#import "SCHomeClassifyView.h"
#import "SCHomeClassifyCell.h"

static NSString *cellIdentifier = @"SCHomeClassifyCell";


@interface SCHomeClassifyView()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic , strong) UICollectionView *collection_view;



@end


@implementation SCHomeClassifyView

-(instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if(self){
        
        [self addSubview:self.collection_view];
    }
    return self;
}

#pragma mark 私有方法
-(UICollectionView *)collection_view{
    
    if (_collection_view == nil) {
        
        CGFloat layout_w = kScreenW / 4;
        CGFloat layout_h = k_classify_view_h / 2;
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(layout_w, layout_h);
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        _collection_view = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, k_classify_view_h) collectionViewLayout:layout];
        _collection_view.backgroundColor = [UIColor whiteColor];
        _collection_view.showsHorizontalScrollIndicator = NO;
        _collection_view.showsVerticalScrollIndicator = NO;
        _collection_view.dataSource = self;
        _collection_view.delegate = self;
        
        [_collection_view registerClass:[SCHomeClassifyCell class] forCellWithReuseIdentifier:cellIdentifier];
        
    }
    
    return _collection_view;
    
}

#pragma mark 对外方法
-(void)setHome_classify_array:(NSMutableArray *)home_classify_array{
    
    _home_classify_array = home_classify_array;
    [self.collection_view reloadData];
    
}

#pragma mark - UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return _home_classify_array.count;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    SCHomeClassifyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    SCHomeClassifyModel *model = _home_classify_array[indexPath.row];
    [cell setClassify_model:model];
    
    
    return cell;
    
}

// 定义上下cell的最小间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    
    return 0;
}

// 定义section的边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    return UIEdgeInsetsMake(0, 0, 0, 0);
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([self.delegate respondsToSelector:@selector(clickHomeClassifyWithIndex:)]) {
        [self.delegate clickHomeClassifyWithIndex:indexPath.row];
    }
}

@end
